﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;

using Common;

namespace Print
{
    /// <summary>
    /// DoAjax 的摘要说明
    /// </summary>
    public class DoAjax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            context.Response.ContentType = "application/json";
            string action = context.Request["act"].ToString();
            string html = "{\"list\":[";
            if (action == "news")
            {
                PBLL.PNews bll = new PBLL.PNews();
                int size = int.Parse(context.Request["size"]);
                int page = int.Parse(context.Request["page"]);
                OleDbDataReader dr = bll.GetPageList(size, page);
                while (dr.Read())
                {
                    html += "{\"id\":\"" + dr["Id"].ToString() + "\",";
                    html += "\"title\":\"" + dr["NTitle"].ToString() + "\",";
                    html += "\"value\":\"" + dr["NHit"].ToString() + "\"},";
                }
                dr.Close();
                html = html.Substring(0, html.Length - 1) + "],\"page\":[{\"cont\":\"";
                int total = bll.GetCount();
                int maxPage = (total % size) == 0 ? (total / size) : (total / size + 1);
                html += page == 0 ? "<li>首页</li>" : "<li class='cpage' onclick='newList(" + size + ", 0)'>首页</li>";
                for (int i = 0; i < maxPage; i++)
                {
                    html += page == i ? "<li class='select'>" + (i + 1) + "</li>" : "<li class='cpage' onclick='newList(" + size + ", " + i + ")'>" + (i + 1) + "</li>";
                }
                html += page == (maxPage - 1) ? "<li>末页</li>" : "<li class='cpage' onclick='newList(" + size + ", " + (maxPage - 1) + ")'>末页</li>";
                html += "<li><span class='pageinfo'>共 <strong>" + maxPage + "</strong>页<strong>" + total + "</strong>条</span></li>";
                html += "\"}]}";
            }
            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}